package org.jet.emall.security.handler;

import com.alibaba.fastjson.JSON;
import org.jet.emall.common.restful.result.CommonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证过的用户访问无权限时被拒绝时的回调，因该在此返回403
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-09 12:10
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        CommonResult<Object> result = CommonResult.forbidden();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
        response.flushBuffer();
    }
}
